<!--
 The MIT License

 Copyright (c) 2017,2018,2019,2020,2021,2022 Klaus Landsdorf (http://node-red.plus/)
 All rights reserved.
 node-red-contrib-bacnet
-->

<script type="text/javascript">
  RED.nodes.registerType('BACnet-Read', {
    category: 'BACnet',
    color: "#16BAC4",
    defaults: {
      name: {value: ''},
      objectType: {value: ''},
      instance: {type: "BACnet-Instance", required:true},
      propertyId: {value: ''},
      device: {type: "BACnet-Device", required:true},
      server: {type: "BACnet-Client", required:true},
      multipleRead: {value: false}
    },
    inputs: 1,
    outputs: 1,
    align: "right",
    icon: "bacnet-icon.png",
    label: function () {
      return this.name || "BACnet Read";
    },
    labelStyle: function () {
      return this.name ? "node_label_italic" : "";
    },
    oneditprepare: function () {
      let node = this

      /*
      let multipleWriteCheckbox = $("#node-input-multipleRead")
      let requestArrayRow = $("#requestArray-row")

      multipleWriteCheckbox.change(function () {
        if ($(this).is(":checked")) {
          requestArrayRow.show();
        } else {
          requestArrayRow.hide();
        }
      });
      */

      let objectTypeLookupField = $("#node-lookup-objectType")
      let objectTypeField = $("#node-input-objectType")

      objectTypeLookupField.click(function () {
        objectTypeLookupField.addClass('disabled')
        $.getJSON('/bacnet/ObjectTypes', function (data) {
          objectTypeLookupField.removeClass('disabled')
          node.lookupItems = []

          $.each(data, function (i, entry) {
            node.lookupItems.push({value: entry.typeValue, label: entry.label})
          })

          objectTypeField.autocomplete({
            source: node.lookupItems,
            minLength: 0,
            focus: function (event, ui) {
              objectTypeField.val(ui.item.value);
              return false;
            },
            select: function (event, ui) {
              objectTypeField.val(ui.item.value)
              return false;
            },
            close: function (event, ui) {
              objectTypeField.autocomplete("destroy")
            }
          }).autocomplete("search", "")
        })
      })

      let datatypeLookupField = $("#node-lookup-propertyId")
      let datatypeField = $("#node-input-propertyId")

      datatypeLookupField.click(function () {
        datatypeLookupField.addClass('disabled')
        $.getJSON('/bacnet/PropertyIds', function (data) {
          datatypeLookupField.removeClass('disabled')
          node.lookupItems = []

          $.each(data, function (i, entry) {
            node.lookupItems.push({value: entry.typeValue, label: entry.label})
          })

          datatypeField.autocomplete({
            source: node.lookupItems,
            minLength: 0,
            focus: function (event, ui) {
              datatypeField.val(ui.item.value);
              return false;
            },
            select: function (event, ui) {
              datatypeField.val(ui.item.value)
              return false;
            },
            close: function (event, ui) {
              datatypeField.autocomplete("destroy")
            }
          }).autocomplete("search", "")
        })
      })
    }
  });
</script>

<script type="text/x-red" data-template-name="BACnet-Read">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
        <input type="text" id="node-input-name" placeholder="">
    </div>
    <div class="form-row">
        <label for="node-input-objectType"><i class="icon-search"></i> <span data-i18n="bacnet-contrib.label.objectType"></span></label>
        <input type="text" id="node-input-objectType" style="width:60%;" placeholder="8">
        <a id="node-lookup-objectType" class="btn"><i id="node-lookup-topic-icon" class="fa fa-search"></i></a>
    </div>
    <div class="form-row">
        <label for="node-input-instance"><i class="icon-globe"></i> <span data-i18n="bacnet-contrib.label.instance"></span></label>
        <input type="text" id="node-input-instance">
    </div>
    <div class="form-row">
        <h4>Properties</h4>
        <hr>
    </div>
    <div class="form-row">
        <label for="node-input-propertyId"><i class="icon-search"></i> <span data-i18n="bacnet-contrib.label.propertyId"></span></label>
        <input type="text" id="node-input-propertyId" style="width:60%;" placeholder="28">
        <a id="node-lookup-propertyId" class="btn"><i id="node-lookup-topic-icon" class="fa fa-search"></i></a>
    </div>
    <div class="form-row">
        <hr>
        <h4>Device and Interface</h4>
        <hr>
    </div>
    <div class="form-row">
        <label for="node-input-device"><i class="icon-globe"></i> <span data-i18n="bacnet-contrib.label.device"></span></label>
        <input type="text" id="node-input-device">
    </div>
    <div class="form-row">
        <label for="node-input-server"><i class="icon-globe"></i> <span data-i18n="bacnet-contrib.label.server"></span></label>
        <input type="text" id="node-input-server">
    </div>
    <hr>
    <div class="form-row">
        <label style="min-width:160px" for="node-input-multipleRead"><i class="fa fa-th"></i>
        <span data-i18n="bacnet-contrib.label.multipleRead"></span></label>
        <input type="checkbox" id="node-input-multipleRead" style="max-width:30px">
    </div>
</script>

<script type="text/x-red" data-help-name="BACnet-Read">
    <h2>BACnet Read node</h2>

    <h3>BETA</h3>
    <a href="https://fh1ch.github.io/node-bacstack/" target="_blank">API Docs</a>
    <p>The msg object properties are used if they filled up with some, otherwise it uses the node settings</p>

    <h3>Multiple Read</h3>
    <h4>Input</h4>
    <p>msg.payload.deviceIPAddress</p>

    <strong>msg.payload.requestArray</strong>
    <i>Array of objects with properties:</i>
    <p>objectId.type</p>
    <p>objectId.instance</p>
    <p>properties</p>
    <i>Array of objects with property:</i>
    <p>id</p>

    <p>Example of msg.payload.requestArray:</p>
    <pre>
        const requestArray = [{
            objectId: {type: 8, instance: 4194303},
            properties: [{id: 8}]
        }]
    </pre>

    <pre>
        {type: 8, instance: 44301}, 28
    </pre>

    <p>msg.payload.propertyId</p>

    <strong>msg.payload.options (all optional)</strong>
    <p>msg.payload.options.maxSegments</p>
    <p>msg.payload.options.maxAdpu</p>
    <p>msg.payload.options.invokeId</p>
    <p>msg.payload.options.arrayIndex</p>

    <h3>Simple Read</h3>
    <h4>Input</h4>
    <p>msg.payload.deviceIPAddress</p>

    <strong>msg.payload.objectId</strong>
    <p>msg.payload.objectId.type</p>
    <p>msg.payload.objectId.instance</p>

    <p>msg.payload.propertyId</p>

    <strong>msg.payload.options (all optional)</strong>
    <p>msg.payload.options.maxSegments</p>
    <p>msg.payload.options.maxAdpu</p>
    <p>msg.payload.options.invokeId</p>
    <p>msg.payload.options.arrayIndex</p>

    <pre>
        objectId: {type: 8, instance: 44301}
        propertyId: 28
    </pre>

    <h3>All Modes</h3>
    <h4>Output</h4>
    <p>msg.payload</p>

</script>
